package com.zbkj.front.controller.merchant;

import com.zbkj.common.model.admin.SystemAdmin;
import com.zbkj.common.model.merchant.Merchant;
import com.zbkj.common.monitorchannel.vo.CtrlPTZRequestVo;
import com.zbkj.common.monitorchannel.GetDeviceModelV3Request;
import com.zbkj.common.monitorchannel.GetMultiMediaAddrV3Request;
import com.zbkj.common.monitorchannel.GetMultiMediaAddrV3Response;
import com.zbkj.common.monitorchannel.vo.CtrlPTZRequestVo;
import com.zbkj.common.monitorchannel.vo.MonitorSetOpenFlagVo;
import com.zbkj.common.monitorchannel.vo.MonitorSetTimeRequestVo;
import com.zbkj.common.monitorchannel.vo.SetDevicesV3Request;
import com.zbkj.common.request.monitor.ZfbMonitorEquipmentAddReq;
import com.zbkj.common.request.monitor.ZfbMonitorEquipmentBindProductReq;
import com.zbkj.common.request.monitor.ZfbMonitorEquipmentDeleteReq;
import com.zbkj.common.request.monitor.ZfbMonitorEquipmentSearchReq;
import com.zbkj.common.response.monitor.BfDeviceInfoResponse;
import com.zbkj.common.result.CommonResult;
import com.zbkj.common.utils.SecurityUtil;
import com.zbkj.service.service.UserService;
import com.zbkj.service.service.ZfbMonitorDeviceBindService;
import com.zbkj.service.service.monitor.IZfbMonitorEquipmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName: MerchantMonitorEquipmentController
 * @Description: TODO
 * @Author: fanbing
 * @Date: 2024/6/29 14:26
 * @Version: V1.0
 **/
@Slf4j
@RestController
@RequestMapping("api/front/merchant/monitor/equipment")
@Api(tags = "移动端商家管理 - 商户端监控设备") //配合swagger使用
public class MerchantMonitorEquipmentController {

    @Autowired
    private IZfbMonitorEquipmentService zfbMonitorEquipmentService;
    @Autowired
    private ZfbMonitorDeviceBindService zfbMonitorDeviceBindService;

    @Autowired
    private UserService userService;

    @ApiOperation(value = "商户监控设备列表") //配合swagger使用
    @PostMapping(value = "/list")
    public CommonResult getList(@RequestBody @Validated ZfbMonitorEquipmentSearchReq searchRequest) {
        SystemAdmin systemAdmin = userService.getSystemAdminByMerchantEmployee();
        return CommonResult.success(zfbMonitorEquipmentService.getMerchantPageList(searchRequest, systemAdmin));
    }


    @ApiOperation(value = "商户新增监控设备") //配合swagger使用
    @PostMapping(value = "/add")
    public CommonResult add(@RequestBody @Validated ZfbMonitorEquipmentAddReq addRequest) {
        SystemAdmin systemAdmin = userService.getSystemAdminByMerchantEmployee();
        return CommonResult.success(zfbMonitorEquipmentService.addMonitorEquipment(addRequest, systemAdmin));
    }

    @ApiOperation(value = "商户修改监控设备") //配合swagger使用
    @PostMapping(value = "/edit")
    public CommonResult edit(@RequestBody @Validated ZfbMonitorEquipmentAddReq addRequest) {
        SystemAdmin systemAdmin = userService.getSystemAdminByMerchantEmployee();
        return CommonResult.success(zfbMonitorEquipmentService.editMonitorEquipment(addRequest, systemAdmin));
    }



    @ApiOperation(value = "商户删除监控设备") //配合swagger使用
    @PostMapping(value = "/delete")
    public CommonResult delete(@RequestBody @Validated ZfbMonitorEquipmentDeleteReq delRequest) {
        SystemAdmin systemAdmin = userService.getSystemAdminByMerchantEmployee();
        return CommonResult.success(zfbMonitorEquipmentService.deleteMonitorEquipment(delRequest, systemAdmin));
    }

    @ApiOperation(value = "商户监控设备绑定商品") //配合swagger使用
    @PostMapping(value = "/bindProduct")
    public CommonResult bindProduct(@RequestBody @Validated ZfbMonitorEquipmentBindProductReq bindProductRequest) {
        SystemAdmin systemAdmin = userService.getSystemAdminByMerchantEmployee();
        return CommonResult.success(zfbMonitorEquipmentService.merchantBindProduct(bindProductRequest, systemAdmin));
    }

    @ApiOperation(value = "查询设备状态")
    @GetMapping(value = "/getEquipmentStatus/{license}")
    public CommonResult getEquipmentStatus(@PathVariable(value = "license") String license) {
        return CommonResult.success(zfbMonitorEquipmentService.getEquipmentStatus(license));
    }

    @ApiOperation(value = "查询设备流媒体地址") //配合swagger使用
    @PostMapping(value = "/getMediaAddress")
    public CommonResult getMediaAddress(@RequestBody @Validated ZfbMonitorEquipmentSearchReq searchRequest) {
        return CommonResult.success(zfbMonitorEquipmentService.getMediaAddress(searchRequest.getLicense()));
    }


    @ApiOperation(value = "ptz控制设备") //配合swagger使用
    @PostMapping(value = "/ctrlPTZ")
    public CommonResult ctrlPTZ(@RequestBody @Validated CtrlPTZRequestVo ctrlPTZRequestVo) {
        return CommonResult.success(zfbMonitorEquipmentService.ctrlPTZ(ctrlPTZRequestVo));
    }

    @ApiOperation(value = "通过该命令向设备设置时间信息") //配合swagger使用
    @PostMapping(value = "/setTime")
    public CommonResult setTime(@RequestBody @Validated MonitorSetTimeRequestVo setTimeRequestVo) {
        return CommonResult.success(zfbMonitorEquipmentService.setTime(setTimeRequestVo));
    }

    @ApiOperation(value = "5.2.2 用户账号添加/删除设备") //配合swagger使用
    @PostMapping(value = "/SetDevices")
    public CommonResult SetDevices(@RequestBody SetDevicesV3Request request) {
        return CommonResult.success(zfbMonitorEquipmentService.SetDevices(request, 0));
    }


    /**
     * 5.5.1.1 查询设备的业务配置
     * 功能说明：
     * 1、根据功能模块类型查询设备的配置和能力；
     * 2、请求 URL；/GMModule/V3/Device/GetDeviceModel;
     *
     * @param request
     * @return
     */
    @ApiOperation(value = "5.5.1.1 查询设备的业务配置") //配合swagger使用
    @PostMapping(value = "/GetDeviceModel")
    public CommonResult GetDeviceModel(@RequestBody GetDeviceModelV3Request request) {
        return CommonResult.success(zfbMonitorEquipmentService.GetDeviceModel(request, 0));
    }


    /**
     * 5.5.8.1 获取直播播放地址
     * 功能说明：
     * 1、获取设备流媒体直播地址；
     * 2、请求路径 url: /GMModule/V3/Media/GetMultiMediaAddr；
     *
     * @param request
     * @return
     */
    @ApiOperation(value = "5.5.8.1 获取直播播放地址") //配合swagger使用
    @PostMapping(value = "/GetMultiMediaAddr")
    public CommonResult GetMultiMediaAddr(@RequestBody GetMultiMediaAddrV3Request request) {
        return CommonResult.success(zfbMonitorEquipmentService.GetMultiMediaAddr(request, 0));
    }


    /**
     * 5.5.4.7 摄像机开关
     * 功能说明：
     * 1、关闭摄像机后，停止所有录制，同时app默认不可以观看，同时镜头转向隐私位；
     * 2、请求URL；/GMModule/V3/Device/SetOpenFlag
     *
     * @param requestVo
     * @return
     */
    @ApiOperation(value = "5.5.4.7 摄像机开关") //配合swagger使用
    @PostMapping(value = "/v3SetOpenFlag")
    public CommonResult v3SetOpenFlag(MonitorSetOpenFlagVo requestVo, Integer equipmentType){
        return CommonResult.success(zfbMonitorEquipmentService.v3SetOpenFlag(requestVo, 0));
    }


    @ApiOperation(value = "获取已绑定的发射器")
    @RequestMapping(value = "/{license}/bf/device", method = RequestMethod.GET)
    public CommonResult<BfDeviceInfoResponse> getBfDevice(@PathVariable(value = "license") String license) {
        SystemAdmin systemAdmin = userService.getSystemAdminByMerchantEmployee();
        return CommonResult.success(zfbMonitorDeviceBindService.getBfDevice(license, systemAdmin.getMerId()));
    }

}
